﻿/*

Copyright 2009 Guillaume Amringer

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

package {
	import caurina.transitions.Tweener;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import adobe.utils.CustomActions;
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.BulkProgressEvent;
	import br.com.stimuli.loading.loadingtypes.LoadingItem;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.external.ExternalInterface;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import gs.TweenMax;
	import lt.uza.utils.Global;
	import events.InterfaceEvent;
	import events.MenuInterfaceEvent;
	import flash.text.Font;
	
	public class Main extends MovieClip {
		
		[Embed(source='assets/mangal.ttf', fontWeight="Regular", fontFamily="Mangal", fontName="Mangal", mimeType="application/x-font-truetype")] 
		public static var _Mangal:Class;
		Font.registerFont(_Mangal);
		
		[Embed(source='assets/mangalb.ttf', fontWeight="Bold", fontFamily="Mangal", fontName="Mangal", mimeType="application/x-font-truetype")] 
		public static var _MangalB:Class;
		Font.registerFont(_MangalB);
		
		public var globals:Global = Global.getInstance();
		
		private var _assetsLoader:BulkLoader = new BulkLoader('assets');
		
		private var _gameInterface:GameInterface;
		private var _loadingInterface:LoadingInterface;
		private var _menuInterface:MenuInterface;
		private var _currentInterface:Interface;
		
		public var game:Game;
		
		public function Main():void {
			
			//	Miscellaneous stuff
			globals.stage = stage;
			globals.width = stage.stageWidth;
			globals.height = stage.stageHeight;
			globals.options = new Object();
			globals.smallTFormat =  new TextFormat('Mangal', 10, 0xffffff);
			globals.normalTFormat =  new TextFormat('Mangal', 14, 0xffffff);
			globals.bigTFormat =  new TextFormat('Mangal', 18, 0xffffff);
			
			//	Set interfaces
			_loadingInterface = new LoadingInterface();
			_menuInterface = new MenuInterface();
			_gameInterface = new GameInterface();
			
			_currentInterface = _loadingInterface;
			
			//	Set stage properties
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.showDefaultContextMenu = false;
			stage.addEventListener(Event.RESIZE, _resizeHandler);
			stage.addEventListener(Event.ENTER_FRAME, _enterFrameHandler)
			
			//	Prep interfaces
			_loadingInterface.addEventListener(InterfaceEvent.READY, _loadingInterfaceReadyHandler);
			_menuInterface.addEventListener(InterfaceEvent.READY, _menuInterfaceReadyHandler);
			_gameInterface.addEventListener(InterfaceEvent.READY, _gameInterfaceReadyHandler);
			
			_menuInterface.addEventListener(MenuInterfaceEvent.ENGAGE_CLICK, _engageClickedHandler);
			_currentInterface.build();
			
			//	Prep the assets loader object
			_assetsLoader.add('C:/Users/wilhelm/test.psd',{id:'track01_normal'});
			_assetsLoader.addEventListener(BulkProgressEvent.PROGRESS, _updateLoadingGUI);
			_assetsLoader.addEventListener(BulkProgressEvent.COMPLETE, _displayMenu);
		}
		
		public function swapInterfaces(pNewInterface:Interface):void {
			var oldInterface:Interface = _currentInterface;
			_currentInterface = pNewInterface;
			_currentInterface.alpha = 0;
			this.addChild(_currentInterface);
			new TweenMax(_currentInterface, 1, { alpha:1, delay:2 } );
			new TweenMax(oldInterface, 1, { alpha:0, onComplete:removeOldInterface, onCompleteParams:new Array(oldInterface) } );
		}
		
		private function removeOldInterface(pInterface:Interface):void {
			this.removeChild(pInterface);
		}
		
		private function _loadingInterfaceReadyHandler(e:InterfaceEvent):void {
			this.addChild(_currentInterface);
			_assetsLoader.start();
		}
		
		private function _menuInterfaceReadyHandler(e:InterfaceEvent):void {
			this.swapInterfaces(_menuInterface);
		}
		
		private function _gameInterfaceReadyHandler(e:InterfaceEvent):void {
			this.swapInterfaces(_gameInterface);
			game = new Game(_gameInterface);
			game.start();
		}
		
		private function _engageClickedHandler(e:MenuInterfaceEvent):void {
			_gameInterface.build();
		}
		
		private function _displayMenu(e:BulkProgressEvent):void {
			_menuInterface.build();
		}
		
		private function _updateLoadingGUI(e:BulkProgressEvent):void {
			_loadingInterface.updatePercentage(_assetsLoader._bytesTotal, _assetsLoader._bytesLoaded, _assetsLoader._itemsTotal, _assetsLoader._itemsLoaded, _assetsLoader._speed, _assetsLoader._percentLoaded);
		}
		
		private function _enterFrameHandler(e:Event):void {
			
		}
		
		private function _resizeHandler(e:Event):void {
			if (globals.stage.stageWidth < Interface.MIN_WIDTH)
				globals.width = Interface.MIN_WIDTH;
			else
				globals.width = globals.stage.stageWidth;
			if (globals.stage.stageHeight < Interface.MIN_HEIGHT)
				globals.height = Interface.MIN_HEIGHT;
			else
				globals.height = globals.stage.stageHeight;
			
			_currentInterface.resize();
		}
	}
}
